#ifndef IMAGEDETECT_H
#define IMAGEDETECT_H

#include <vector>
#include <opencv/cv.h>
#include <mutex>

struct Blob
{
    cv::Point2d location;
    double area;
    double radius;
    int sameLocationCount = 0;
};

struct FrameInf
{
    std::vector<Blob> blobs;
    timeval timestamp = {0,0};
    bool hasSuspectedTarget = false;
};

class ImageDetect
{
public:
    static ImageDetect& GetInstance();//获取实例
    ImageDetect& operator = (const ImageDetect&) = delete;
    ImageDetect(const ImageDetect&) = delete;
//    ~ImageDetect();
private:
    static ImageDetect m_Instance;
    ImageDetect();

public:
    void DetectThread();
    std::vector<cv::Point2d> GetObjectPoints();

public:
    std::mutex m_dstImageMtx;
    cv::Mat m_dstImage;

private:
    void Detect();
    void FrameDetect(cv::Mat &img);
    void DetectBigBlobs(std::vector<std::vector<cv::Point>> &contours);//找到大的斑点，删去太靠近的斑点
    void TargetSameLocationConfirm();
    void UpdateObjectPoints();
    void UpdateFrameInfVec();
    void CheckTargetLost();
    void CamGainAutoSet();//根据图中白点数目以及检测到的白色斑点数量、大小调整增益
    void ShowImage(const cv::Mat &image);

private:
    const unsigned int BLOB_NUM_MAX = 6;
    const unsigned int TARGET_NUM_MAX = 2;
    const int BLOB_AREA_MIN = 15;//blob尺寸大于这个值的为疑似目标，待后面判断确认是否是真的目标
    const int BLOB_AREA_CONFIRM = 100;//blob尺寸大于这个值，则明确当前帧有目标
    const int WHITE_PIX_NUM_CONFIRM = 120;//白点数大于这个值，则明确当前帧有目标
    //华诺控制光电球转动一圈360°耗时45秒，即1秒转8°，镜头视场为33°，即1秒转0.24个视场，对应最大172.8个像素
     //1秒25帧，每帧最多转7个像素,考虑有可能会转一圈耗时20秒以内，每帧转动像素余量最多为SAME_POSITION_DIST
    const unsigned int SAME_POSITION_DIST = 25;
    const int WIDTH = 720;
    const int HEIGHT = 576;
    const int FRAME_COUNT_SIZE = 26;//1秒内的帧数
    const int SAME_POSITION_FRAME_NUM = 2;//前后帧判断同一个位置所需要的的帧数，不包括当前帧
    std::mutex m_objectPointsMtx;// 同步锁
    std::vector <cv::Point2d> m_objectPoints;//发送给外界的目标信息
    int m_whitePixNum;
    FrameInf m_frameInf;
    std::vector<FrameInf> m_frameInfVec;
    bool m_targetIsLost = true;

};
//extern ImageDetect imgdetect;
#endif // IMAGEDETECT_H
